package mark.tom.fiter;

import androidx.annotation.NonNull;

import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

public class GsonDataResolver implements IDataResolver {

    @Override
    public Object parseObject(@NonNull String data, @NonNull Class<?> clazz) {
        return new Gson().fromJson(data, clazz);
    }

    @Override
    public List<Object> parseArray(@NonNull String data, @NonNull final Class<?> clazz) {
        return new Gson().fromJson(data, new ParameterizedTypeImpl(clazz));
    }

    private static class ParameterizedTypeImpl implements ParameterizedType {
        Class<?> clazz;

        public ParameterizedTypeImpl(Class<?> clz) {
            clazz = clz;
        }

        @NotNull
        @Override
        public Type[] getActualTypeArguments() {
            return new Type[]{clazz};
        }

        @NotNull
        @Override
        public Type getRawType() {
            return List.class;
        }

        @Override
        public Type getOwnerType() {
            return null;
        }
    }
}
